package demo1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Author liangzai
 * @Description:
 */
public class Test {
    public static void main(String[] args) {
        HashMap<Character,Integer> map1 = new HashMap<>();
        HashMap<Character,Integer> map2 = new HashMap<>();
        map1.put('a',1);
        map2.put('a',2);
        System.out.println(map1.equals(map2));
    }

    //找到字符串中的所有异位词
    public int[] findAnagrams(String s, String p) {
        HashMap<Character,Integer> map1 = new HashMap<>();
        HashMap<Character,Integer> map2 = new HashMap<>();
        System.out.println(map1.equals(map2));
        int[] array = new int[s.length()];
        int count = 0;
        for (int i = 0; i < p.length(); i++) {
            char ch = p.charAt(i);
            if(map1.containsKey(ch)){
                int val = map1.get(ch);
                map1.put(ch,val+1);
            }else {
                map1.put(ch,1);
            }
        }
        for(int left=0,right=0;right<s.length();){
            if (map2.size() != 3) {
                char ch = s.charAt(right);
                if(map2.containsKey(ch)){
                    int val = map2.get(ch);
                    map2.put(ch,val+1);
                }else {
                    map2.put(ch,1);
                }
                right++;
            }else {
                int val = map2.get(s.charAt(left));
                if (val - 1 == 0) {
                    map2.remove(s.charAt(left));
                }else {
                    map2.put(s.charAt(left),val-1);
                }
                char ch = s.charAt(right);
                if(map2.containsKey(ch)){
                    int val1 = map2.get(ch);
                    map2.put(ch,val1+1);
                }else {
                    map2.put(ch,1);
                }
                right++;

            }
            if(map1.equals(map2)){
                array[count++] = left;
            }
        }
        return Arrays.copyOf(array,count);
    }

    public int maximumGap(int[] nums) {
        int sz = nums.length;
        if (sz < 2) {
            return 0;
        }
        Arrays.sort(nums);
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < sz-1; i++) {
            max = Math.max(max,nums[i+1]-nums[i]);
        }
        return max;
    }

    public ListNode rotateRight(ListNode head, int k) {
        ListNode pcur = head;
        int sz = 0;
        while (pcur != null) {
            sz++;
            pcur = pcur.next;
        }
        int n = k%sz;
        if (n == 0) {
            return head;
        }
        pcur = head;
        ListNode pcurN = pcur;
        ListNode prev = head;
        ListNode prevN = prev;
        for (int i = 0; i < n; i++) {
            pcur = pcur.next;
        }
        while (pcur != null) {
            pcurN = pcur;
            pcur = pcur.next;
            prevN = prev;
            prev = prev.next;
        }
        prevN.next = null;
        pcurN.next = head;
        return prev;
    }

    private void rotateRightChild(){

    }

    public int minOperations(int[] nums) {
        int sz = nums.length;
        int count = 0;
        for (int i = sz-2; i <=0 ; i--) {
            if(nums[i] != nums[i+1]){
                count++;
            }
        }
        return nums[0]==1?count:count++;
    }

    public int minOperations1(int[] nums) {
        int n = nums.length;
        int pcur = 0;
        int count = 0;
        while (pcur < n) {
            if (nums[pcur] != 1) {
                for (int i = pcur; i < n; i++) {
                    nums[i] ^= 1;
                }
                count++;
            }
            pcur++;
        }
        return count;
    }
}
